package cate.game.activity.heroroad;

import cate.common.table.activity.heroroad.row.HeroRoadBaseRow;
import cate.common.table.activity.heroroad.row.HeroRoadRewardRow;
import cate.common.table.d.GDFunc;
import cate.common.util.GameResult;
import cate.game.activity.base.core.RoleActivitySingle;
import cate.game.activity.heroroad.msg.HeroRoadDataResp;
import cate.game.res.MixRes;
import cate.game.res.MixResAdder;
import cate.game.role.Role;
import cate.game.role.task.TaskGroup;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;

import java.util.*;

@NoteClass("英雄之路数据")
public class HeroRoad extends RoleActivitySingle {
    @NoteField("领取记录")
    public Map<Integer, Boolean> gotMap;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (gotMap == null) {
            gotMap = new HashMap<>();
        }
    }

    @Override
    public void onRoleOpen() {
    }

    @Override
    public void onRoleEnd() {
        gotMap.clear();
    }

    @Override
    public void initSend() {
        noticeUpdate();
    }

    private HeroRoadBaseRow baseRow() {
        return role.getGame().table.heroRoad.base.get(configTid());
    }

    private HeroRoadRewardRow rewardRow(int tid) {
        return role.getGame().table.heroRoad.reward.get(tid);
    }

    @Override
    public void excelCheck() {
        HeroRoadBaseRow baseRow = baseRow();
        if (baseRow == null || (baseRow.taskGroupId <= 0 && baseRow.payTaskGroupId <= 0)) {
            role.getTask().getGroup(funcId).clear();
            role.getTask().getGroup(GDFunc.HERO_ROAD_PAY).clear();
            return;
        }
        if (baseRow.taskGroupId > 0) {
            List<Integer> taskIds= role.getGame().table.heroRoad.task.getDayTaskIds(baseRow.taskGroupId, getCurrentDay());
            removeAndAddTasks(taskIds, funcId);
        } else {
            role.getTask().getGroup(funcId).clear();
        }
        if (baseRow.payTaskGroupId > 0) {
            List<Integer> taskIds = role.getGame().table.heroRoad.payTask.getTaskIds(baseRow.payTaskGroupId);
            removeAndAddTasks(taskIds, GDFunc.HERO_ROAD_PAY);
        } else {
            role.getTask().getGroup(GDFunc.HERO_ROAD_PAY).clear();
        }
    }

    private void removeAndAddTasks(List<Integer> taskIds, int funcId){
        TaskGroup taskGroup = role.getTask().getGroup(funcId);

        // 删除不存在的任务
        List<Integer> delIds = new ArrayList<>();
        for (int taskId : taskGroup.taskIdSet) {
            if (!taskIds.contains(taskId)) {
                delIds.add(taskId);
            }
        }
        if (!delIds.isEmpty()) {
            taskGroup.removeItemList(delIds);
        }
        taskGroup.addItemListNoRepeat(taskIds);
    }

    public GameResult<Void> gotReward(int tid) {
        GameResult<Void> r = new GameResult<>();
        HeroRoadBaseRow baseRow = baseRow();
        if (baseRow == null) {
            return r.fail("功能未开启");
        }
        if (gotMap.containsKey(tid)) {
            return r.fail("奖励已领取");
        }
        HeroRoadRewardRow rewardRow = rewardRow(tid);
        if (rewardRow == null) {
            return r.fail("奖励不存在");
        }
        if (rewardRow.group != baseRow.rewardGroupId) {
            return r.fail("无法领取该奖励");
        }
        long count = role.getBag().numRes.count(baseRow.rewardTid);
        if (count < rewardRow.value) {
            return r.fail("未达到奖励领取条件");
        }
        gotMap.put(tid, true);
        new MixResAdder()
                .setRes(new MixRes(rewardRow.rewardStr))
                .setSendMailWhenFull(true)
                .setDialogReward(true)
                .exe(role);
        noticeUpdate();
        return r;
    }

    public void noticeUpdate() {
        role.sendNow(new HeroRoadDataResp(this));
    }

    @Override
    public void onDaySpan(boolean silence) {
        super.onDaySpan(silence);
        excelCheck();
        if (!silence) {
            noticeUpdate();
        }
    }
}
